package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.SchoolTeacherDuty;

public class SchoolTeacherDutyRowMapper implements RowMapper<SchoolTeacherDuty>{
    private int type;
    public static final int AND_TEACHERNAME = 1;
    
    public SchoolTeacherDutyRowMapper() {
    }

    public SchoolTeacherDutyRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolTeacherDuty mapRow(ResultSet rs, int rowNum) throws SQLException {
        SchoolTeacherDuty std = null;
        switch (type) {
            case AND_TEACHERNAME:
                std = parseSchoolTeacherDuty(new SchoolTeacherDuty(), rs, "std");
                std.setTeacherName(rs.getString("teacherName"));
                std.setTypeName(rs.getString("typeName"));
                std.settAccount(rs.getString("tAccount"));
                std.setSdate_str(SystemDateFormatUtil.formatDate(std.getSdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                std.setEdate_str(SystemDateFormatUtil.formatDate(std.getEdate(), SystemDateFormatUtil.FORMAT_DATETIME));
                break;
            default:
                std = parseSchoolTeacherDuty(new SchoolTeacherDuty(), rs);
        }
        return std;
    }

    public static SchoolTeacherDuty parseSchoolTeacherDuty(SchoolTeacherDuty s, ResultSet rs) throws SQLException {
        return parseSchoolTeacherDuty(s, rs, null);
    }

    public static SchoolTeacherDuty parseSchoolTeacherDuty(SchoolTeacherDuty s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setDid(rs.getLong(su+"did"));
        } catch (SQLException e) {
            s.setDid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            s.setState(null);
        }
        try {
            s.setSdate(rs.getTimestamp(su+"sdate"));
        } catch (SQLException e) {
            s.setSdate(null);
        }
        try {
            s.setEdate(rs.getTimestamp(su+"edate"));
        } catch (SQLException e) {
            s.setEdate(null);
        }
    
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolTeacherDuty s) {
        par.put("sid", s.getSid());
        par.put("did",s.getDid());
        par.put("tid",s.getTid());
        par.put("state",s.getState());
        par.put("edate",s.getEdate());
        par.put("sdate", s.getSdate());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolTeacherDuty s) {
        args.put("did",s.getDid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolTeacherDuty s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "did" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolTeacherDuty s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_teacher_duty(");
        int count = 0;
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }

        if (null !=s.getState()) {
            sql.append("state,");
            count++;
        }
        if (null !=s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null !=s.getEdate()) {
            sql.append("edate,");
            count++;
        }
        if (null !=s.getSdate()) {
            sql.append("sdate,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolTeacherDuty s)
        throws SQLException {
        int count = 1;
        if (null != s.getSid()) {
            ps.setInt(count++,s.getSid());
        }
        if (null != s.getState()) {
            ps.setShort(count++, s.getState());
        }
        if (null != s.getTid()) {
            ps.setLong(count++, s.getTid());
        }
        if (null != s.getEdate()) {
            ps.setTimestamp(count++, s.getEdate());
        }
        if (null != s.getSdate()) {
            ps.setTimestamp(count++, s.getSdate());
        }



    }
}
